package com.shaoshaohuo.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TabHost;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.OrderListAdapter;
import com.shaoshaohuo.app.adapter.OrderShipperListAdapter;
import com.shaoshaohuo.app.adapter.PurchaseOrderListAdapter;
import com.shaoshaohuo.app.adapter.SupplyOrderListAdapter;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.constant.ListViewAction;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.Order;
import com.shaoshaohuo.app.entity.OrderListEntity;
import com.shaoshaohuo.app.entity.PurchaseOrder;
import com.shaoshaohuo.app.entity.PurchaseOrderEntity;
import com.shaoshaohuo.app.entity.SupplyOrder;
import com.shaoshaohuo.app.entity.SupplyOrderEntity;
import com.shaoshaohuo.app.framework.IViewFramework;
import com.shaoshaohuo.app.framework.SharedPreferencesHelper;
import com.shaoshaohuo.app.net.HttpRequest;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.ec.PurchaseOrderDetailActivity;
import com.shaoshaohuo.app.ui.ec.PurchaseRecordDetailActivity;
import com.shaoshaohuo.app.ui.ec.SupplyOrderDetailActivity;
import com.shaoshaohuo.app.ui.shipper.OrderDetailShipperActivity;
import com.shaoshaohuo.app.utils.StringUtil;
import com.shaoshaohuo.app.utils.ViewHelper;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的订单
 * Created by YinSF on 16/1/26.
 */
public class OrderManagerActivity extends BaseActivity implements IViewFramework, View.OnClickListener,
        TabHost.OnTabChangeListener, XListView.IXListViewListener, AdapterView.OnItemClickListener  {

    //物流接口 状态
    private static final int STATUS_IN_PROGRESS = 2;
    private static final int STATUS_OVERDONE = 3;
    private static final int STATUS_INVALID = 5;
    //电商接口 状态
    public static final int STATUS_DAIFUKUAN = 1;
    public static final int STATUS_DAIFAHUO = 2;
    public static final int STATUS_YISHOUHUO = 3;
    public static final int STATUS_FINISH = 4;
    //供应订单 状态  finish 与待付款，与上重用
    public static final int STATUS_DAISHOUHUO = 3;

    private String mArgument;
    public static final String ARGUMENT = "argument";

    /**
     * 采购订单
     */
    public static int ORDER_TYPE_CAIGOU = 1;
    /**
     * 供应订单
     */
    public static int ORDER_TYPE_GONGYING = 2;
    /**
     * 车主订单
     */
    public static int ORDER_TYPE_CHEZHU = 3;
    /**
     * 货主订单
     */
    public static int ORDER_TYPE_HUOZHU = 4;

    //物流部分
    public static final String TAB_WL_INPROGRESS = "TAB_WL_INPROGRESS";
    public static final String TAB_WL_OVER_DONE = "TAB_WL_OVER_DONE";
    public static final String TAB_WL_INVALID = "TAB_WL_INVALID";

    //电商部分
    public static final String TAB_DS_WAIT_PAY = "TAB_DS_WAIT_PAY";//待付款
    public static final String TAB_DS_WAIT_SEND = "TAB_DS_WAIT_SEND";//待发货
    public static final String TAB_DS_HAVE_SEND = "TAB_DS_HAVE_SEND";//已发货
    public static final String TAB_DS_OVER_DONE = "TAB_DS_OVER_DONE";//已完成

    private String[] tabArray = {TAB_DS_WAIT_PAY, TAB_DS_WAIT_SEND, TAB_DS_HAVE_SEND, TAB_DS_OVER_DONE,
            TAB_WL_INPROGRESS, TAB_WL_OVER_DONE, TAB_WL_INVALID};

    private TabHost tabHost;
    private TextView textCaiGou;
    private TextView textGongYing;
    private TextView textHuoZhu;
    private TextView textCheZhu;

    private XListView mOrderListWlInProgressview;
    private XListView mOrderListWlOverDoneview;
    private XListView mOrderListWlInvalidview;

    private XListView mOrderListEcWaitToPay;
    private XListView mOrderListEcWaitSend;
    private XListView mOrderListEcHaveSend;
    private XListView mOrderListEcOverDone;

    private OrderShipperListAdapter<Order> mAdapterCarHuoZhu;
    private OrderListAdapter<Order> mAdapterCarCheZhu;

    private PurchaseOrderListAdapter<PurchaseOrder> mAdapterCaiGou;//采购
    private SupplyOrderListAdapter<SupplyOrder> mAdapterGongYing;//供应


    private int size = 10;
    //物流
    private List<Order> mDataListWuLiu = new ArrayList<Order>();
    //采购
    private List<PurchaseOrder> mDataListCaiGou = new ArrayList<PurchaseOrder>();
    //供应
    private List<SupplyOrder> mDataListGongYing = new ArrayList<SupplyOrder>();

    private String action = ListViewAction.NEW;
    private String actionGongYing = ListViewAction.NEW2;
    private String cursor = "0";
    private int searchstatus;


    //当前的订单类型：采购、供应、车主、货主
    private int currentOrderType = ORDER_TYPE_CAIGOU;
    //当前的Tag标记
    private String currentTabTag = TAB_DS_WAIT_PAY;
    private LinearLayout emptyDsHaveSend;
    private LinearLayout emptyDsOverDone;
    private LinearLayout emptyDstoPay;
    private LinearLayout emptyDsToSend;
    private LinearLayout emptyWlInprogress;
    private LinearLayout emptyWlInvalid;
    private LinearLayout emptyWlOverDone;

    //用于外部跳转目标
    public static final String EXTRA_ORDER_TYPE = "EXTRA_ORDER_TYPE";//订单类型
    public static final String EXTRA_TAB_TAG = "EXTRA_TAB_TAG";//选择的标签

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_orders);

        init();
    }

    @Override
    public void init() {
        initView();
        initTabHost();
        initListener();
    }

    private void initTabHost() {
        int extraOrderType = getIntent().getIntExtra(EXTRA_ORDER_TYPE, -1);
        String extraTabTag = getIntent().getStringExtra(EXTRA_TAB_TAG);
        if (extraOrderType != -1 && !StringUtil.isEmpty(extraTabTag)) {
            currentOrderType = extraOrderType;
            currentTabTag = extraTabTag;
        } else {
            int latestOrderType = SharedPreferencesHelper.getInt(SharedPreferencesHelper.Field.LATEST_MY_ORDER_TYPE, 0);
            if (latestOrderType != 0) {
                currentOrderType = latestOrderType;
            }
            String latestTabTag = SharedPreferencesHelper.getString(SharedPreferencesHelper.Field.LATEST_MY_ORDER_TAG, "");
            if (!StringUtil.isEmpty(latestTabTag)) {
                currentTabTag = latestTabTag;
            }
        }

        TabHost.TabSpec tab1 = tabHost.newTabSpec(TAB_DS_WAIT_PAY);
        tab1.setIndicator("待付款");
        tab1.setContent(R.id.linearLayout_wait_toPay);
        tabHost.addTab(tab1);
        TabHost.TabSpec tab2 = tabHost.newTabSpec(TAB_DS_WAIT_SEND);
        tab2.setIndicator("待发货");
        tab2.setContent(R.id.linearLayout_wait_send);
        tabHost.addTab(tab2);
        TabHost.TabSpec tab3 = tabHost.newTabSpec(TAB_DS_HAVE_SEND);
        tab3.setIndicator("已发货");
        tab3.setContent(R.id.linearLayout_have_send);
        tabHost.addTab(tab3);
        TabHost.TabSpec tab4 = tabHost.newTabSpec(TAB_DS_OVER_DONE);
        tab4.setIndicator("已完成");
        tab4.setContent(R.id.linearLayout_overDone_ec);
        tabHost.addTab(tab4);

        TabHost.TabSpec tab5 = tabHost.newTabSpec(TAB_WL_INPROGRESS);
        tab5.setIndicator("进行中");
        tab5.setContent(R.id.linearLayout_inProgress);
        tabHost.addTab(tab5);
        TabHost.TabSpec tab6 = tabHost.newTabSpec(TAB_WL_OVER_DONE);
        tab6.setIndicator("已完成");
        tab6.setContent(R.id.linearLayout_overDone);
        tabHost.addTab(tab6);
        TabHost.TabSpec tab7 = tabHost.newTabSpec(TAB_WL_INVALID);
        tab7.setIndicator("已失效");
        tab7.setContent(R.id.linearLayout_invalid);
        tabHost.addTab(tab7);

        tabHost.setCurrentTabByTag(currentTabTag);

        selectTabhost(currentOrderType, currentTabTag);
    }

    /**
     * 更新选择订单类型、标签
     * @param newOrderType
     * @param newTabTag
     */
    private void selectTabhost(int newOrderType, String newTabTag) {
        if (newOrderType == currentOrderType && newTabTag == currentTabTag) {
            ViewHelper.setTabHostStyle(tabHost.getTabWidget(), this, getTabIndex(newTabTag));
            resetTabHost(newOrderType);
            selectOrderType(newOrderType);
            onRefresh();
            return;
        } else if (newOrderType == currentOrderType || (newOrderType != currentOrderType && isSameOrderType(newOrderType, currentOrderType))) {
            //只更新数据，不用重置tab
        } else {
            //需要重置tab
//            tabHost.clearAllTabs();
            resetTabHost(newOrderType);
        }

        if (newOrderType != currentOrderType && !isSameOrderType(newOrderType, currentOrderType)) {
            if (newOrderType == ORDER_TYPE_CAIGOU || newOrderType == ORDER_TYPE_GONGYING) {
                newTabTag = TAB_DS_WAIT_PAY;
            } else if (newOrderType == ORDER_TYPE_CHEZHU || newOrderType == ORDER_TYPE_HUOZHU) {
                newTabTag = TAB_WL_INPROGRESS;
            }
        }

        if (newOrderType != currentOrderType) {
            selectOrderType(newOrderType);
        }

        ViewHelper.setTabHostStyle(tabHost.getTabWidget(), this, getTabIndex(newTabTag));

        currentOrderType = newOrderType;
        currentTabTag = newTabTag;
        tabHost.setCurrentTabByTag(currentTabTag);
        SharedPreferencesHelper.setInt(SharedPreferencesHelper.Field.LATEST_MY_ORDER_TYPE, currentOrderType);
        SharedPreferencesHelper.setString(SharedPreferencesHelper.Field.LATEST_MY_ORDER_TAG, currentTabTag);

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                onRefresh();
            }
        }, 200);
    }
    private void resetTabHost(int newOrderType) {
        if (newOrderType == ORDER_TYPE_CAIGOU || newOrderType == ORDER_TYPE_GONGYING) {
            tabHost.getTabWidget().getChildTabViewAt(0).setVisibility(View.VISIBLE);
            tabHost.getTabWidget().getChildTabViewAt(1).setVisibility(View.VISIBLE);
            tabHost.getTabWidget().getChildTabViewAt(2).setVisibility(View.VISIBLE);
            tabHost.getTabWidget().getChildTabViewAt(3).setVisibility(View.VISIBLE);
            tabHost.getTabWidget().getChildTabViewAt(4).setVisibility(View.GONE);
            tabHost.getTabWidget().getChildTabViewAt(5).setVisibility(View.GONE);
            tabHost.getTabWidget().getChildTabViewAt(6).setVisibility(View.GONE);
        } else {
            tabHost.getTabWidget().getChildTabViewAt(0).setVisibility(View.GONE);
            tabHost.getTabWidget().getChildTabViewAt(1).setVisibility(View.GONE);
            tabHost.getTabWidget().getChildTabViewAt(2).setVisibility(View.GONE);
            tabHost.getTabWidget().getChildTabViewAt(3).setVisibility(View.GONE);
            tabHost.getTabWidget().getChildTabViewAt(4).setVisibility(View.VISIBLE);
            tabHost.getTabWidget().getChildTabViewAt(5).setVisibility(View.VISIBLE);
            tabHost.getTabWidget().getChildTabViewAt(6).setVisibility(View.VISIBLE);
        }
    }


    /**
     * 选择订单类型TextView
     * @param newOrderType
     */
    private void selectOrderType(int newOrderType) {
        textCaiGou.setSelected(false);
        textGongYing.setSelected(false);
        textCheZhu.setSelected(false);
        textHuoZhu.setSelected(false);
        if (newOrderType == ORDER_TYPE_CAIGOU) {
            textCaiGou.setSelected(true);
        } else if (newOrderType == ORDER_TYPE_GONGYING) {
            textGongYing.setSelected(true);
        } else if (newOrderType == ORDER_TYPE_CHEZHU) {
            textCheZhu.setSelected(true);
        } else if (newOrderType == ORDER_TYPE_HUOZHU) {
            textHuoZhu.setSelected(true);
        }
    }

    /**
     * 根据tabTag，获取序号
     * @param newTabTag
     * @return
     */
    private int getTabIndex(String newTabTag) {
        for (int i = 0; i < tabArray.length; i++) {
            String tag = tabArray[i];
            if (tag.equals(newTabTag)) {
                return i;
            }
        }

        return 0;
    }

    /**
     * 判断是否同为电商、或物流订单类型
     * @param newType
     * @param oldType
     * @return
     */
    private boolean isSameOrderType(int newType, int oldType) {
        if ((newType == ORDER_TYPE_CAIGOU && oldType == ORDER_TYPE_GONGYING)
                || (newType == ORDER_TYPE_GONGYING && oldType == ORDER_TYPE_CAIGOU)) {
            return true;
        } else if ((newType == ORDER_TYPE_CHEZHU && oldType == ORDER_TYPE_HUOZHU)
                || (newType == ORDER_TYPE_HUOZHU && oldType == ORDER_TYPE_CHEZHU)) {
            return true;
        }
        return false;
    }

    @Override
    public void initView() {
        TopbarView topBar = (TopbarView) findViewById(R.id.topbar);
        topBar.setCenterText("我的订单");
        topBar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        textCheZhu = (TextView) findViewById(R.id.textview_chezhu);
        textHuoZhu = (TextView) findViewById(R.id.textview_huozhu);
        textCaiGou = (TextView) findViewById(R.id.textview_caigou);
        textGongYing = (TextView) findViewById(R.id.textview_gongying);

        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();

        mOrderListWlInProgressview = (XListView) findViewById(R.id.listview_list_inProgress);
        mOrderListWlOverDoneview = (XListView) findViewById(R.id.listview_list_overDone);
        mOrderListWlInvalidview = (XListView) findViewById(R.id.listview_list_invalid);

        mOrderListEcWaitToPay = (XListView) findViewById(R.id.listview_list_wait_toPay);
        mOrderListEcWaitSend = (XListView) findViewById(R.id.listview_list_wait_send);
        mOrderListEcHaveSend = (XListView) findViewById(R.id.listview_list_have_send);
        mOrderListEcOverDone = (XListView) findViewById(R.id.listview_list_overDone_ec);

        emptyDsHaveSend = (LinearLayout) findViewById(R.id.empty_ds_haveSend);
        emptyDsOverDone = (LinearLayout) findViewById(R.id.empty_ds_overDone);
        emptyDstoPay = (LinearLayout) findViewById(R.id.empty_ds_toPay);
        emptyDsToSend = (LinearLayout) findViewById(R.id.empty_ds_toSend);
        emptyWlInprogress = (LinearLayout) findViewById(R.id.empty_wl_inprogress);
        emptyWlInvalid = (LinearLayout) findViewById(R.id.empty_wl_invalid);
        emptyWlOverDone = (LinearLayout) findViewById(R.id.empty_wl_overDone);
    }

    @Override
    public void initData() {
        if (textCheZhu.isSelected()) {
            getOrderListCheZhu(true);
        } else if (textHuoZhu.isSelected()) {
            getOrderListHuoZhu(true);
        } else if (textCaiGou.isSelected()) {
            getOrderListCaiGou(true);
        } else if (textGongYing.isSelected()) {
            getOrderListGongYing(true);
        }
    }

    private void getOrderListGongYing(boolean isLoading) {
        if (isLoading) {
            startLoadingDialog();
        }

        RequestService.getInstance().getSupplyOrderList(this, actionGongYing, cursor, size + "", searchstatus + "", SupplyOrderEntity.class,
                new HttpRequest.RequestListener() {

                    @Override
                    public void onSuccess(int requestCode, BaseEntity resultData) {
                        dismissLoadingDialog();
                        if (resultData.isOk()) {
                            SupplyOrderEntity entity = (SupplyOrderEntity) resultData;
                            List<SupplyOrder> list = entity.getData().getList();
                            mDataListGongYing.addAll(list);

                            if ((mDataListGongYing.size() < size)
                                    || (actionGongYing.equals(ListViewAction.HISTORY2) && list.size() < size)) {
                                if (TAB_DS_OVER_DONE.equals(currentTabTag)) {
                                    mOrderListEcOverDone.endLoadingMore("没有更多了");
                                } else if (TAB_DS_HAVE_SEND.equals(currentTabTag)) {
                                    mOrderListEcHaveSend.endLoadingMore("没有更多了");
                                } else if (TAB_DS_WAIT_SEND.equals(currentTabTag)) {
                                    mOrderListEcWaitSend.endLoadingMore("没有更多了");
                                } else if (TAB_DS_WAIT_PAY.equals(currentTabTag)) {
                                    mOrderListEcWaitToPay.endLoadingMore("没有更多了");
                                }
                            }
                            setAdapterGongYing(mDataListGongYing);
                        } else {
                            showToast(resultData.getMsg());
                        }
                        onLoad();
                    }

                    @Override
                    public void onFailed(int requestCode, Exception error, String msg) {
                        dismissLoadingDialog();
                        showToast(msg);
                        onLoad();
                    }
                });
    }

    private void setAdapterGongYing(List<SupplyOrder> mDataList2) {
        if (mAdapterGongYing == null) {
            mAdapterGongYing = new SupplyOrderListAdapter<SupplyOrder>(this, mDataList2, false);
        } else {
            mAdapterGongYing.setData(mDataList2);
        }
        refreshDsAdapter(mAdapterGongYing);
    }

    private void getOrderListCaiGou(boolean isLoading) {
        if (isLoading) {
            startLoadingDialog();
        }
        RequestService.getInstance().getPurchaseOrderList(this, searchstatus + "", cursor, size + "", action, PurchaseOrderEntity.class,
                new HttpRequest.RequestListener() {

                    @Override
                    public void onSuccess(int requestCode, BaseEntity resultData) {
                        dismissLoadingDialog();
                        if (resultData.isOk()) {
                            PurchaseOrderEntity entity = (PurchaseOrderEntity) resultData;
                            List<PurchaseOrder> list = entity.getData().getList();
                            mDataListCaiGou.addAll(list);

                            if ((mDataListCaiGou.size() < size) || (action.equals(ListViewAction.HISTORY) && list.size() < size)) {

                                if (TAB_DS_OVER_DONE.equals(currentTabTag)) {
                                    mOrderListEcOverDone.endLoadingMore("没有更多了");
                                } else if (TAB_DS_HAVE_SEND.equals(currentTabTag)) {
                                    mOrderListEcHaveSend.endLoadingMore("没有更多了");
                                } else if (TAB_DS_WAIT_SEND.equals(currentTabTag)) {
                                    mOrderListEcWaitSend.endLoadingMore("没有更多了");
                                } else if (TAB_DS_WAIT_PAY.equals(currentTabTag)) {
                                    mOrderListEcWaitToPay.endLoadingMore("没有更多了");
                                }
                            }
                            setAdapterCaiGou(mDataListCaiGou);

                        } else {
                            showToast(resultData.getMsg());
                        }
                        onLoad();
                    }

                    @Override
                    public void onFailed(int requestCode, Exception error, String msg) {
                        dismissLoadingDialog();
                        showToast(msg);
                        onLoad();
                    }
                });
    }

    private void setAdapterCaiGou(List<PurchaseOrder> mDataList2) {
        if (mAdapterCaiGou == null) {
            mAdapterCaiGou = new PurchaseOrderListAdapter<PurchaseOrder>(this, mDataList2, false);
        } else {
            mAdapterCaiGou.setData(mDataList2);
        }
        refreshDsAdapter(mAdapterCaiGou);
    }

    private void refreshDsAdapter(ListAdapter la) {
        if (TAB_DS_WAIT_PAY.equals(currentTabTag)) {
            if (la.getCount() == 0) {
                emptyDstoPay.setVisibility(View.VISIBLE);
                mOrderListEcWaitToPay.setVisibility(View.GONE);
            } else {
                emptyDstoPay.setVisibility(View.GONE);
                mOrderListEcWaitToPay.setVisibility(View.VISIBLE);
                mOrderListEcWaitToPay.setAdapter(la);
            }
        } else if (TAB_DS_WAIT_SEND.equals(currentTabTag)) {
            if (la.getCount() == 0) {
                emptyDsToSend.setVisibility(View.VISIBLE);
                mOrderListEcWaitSend.setVisibility(View.GONE);
            } else {
                emptyDsToSend.setVisibility(View.GONE);
                mOrderListEcWaitSend.setVisibility(View.VISIBLE);
                mOrderListEcWaitSend.setAdapter(la);
            }
        } else if (TAB_DS_HAVE_SEND.equals(currentTabTag)) {
            if (la.getCount() == 0) {
                emptyDsHaveSend.setVisibility(View.VISIBLE);
                mOrderListEcHaveSend.setVisibility(View.GONE);
            } else {
                emptyDsHaveSend.setVisibility(View.GONE);
                mOrderListEcHaveSend.setVisibility(View.VISIBLE);
                mOrderListEcHaveSend.setAdapter(la);
            }
        } else if (TAB_DS_OVER_DONE.equals(currentTabTag)) {
            if (la.getCount() == 0) {
                emptyDsOverDone.setVisibility(View.VISIBLE);
                mOrderListEcOverDone.setVisibility(View.GONE);
            } else {
                emptyDsOverDone.setVisibility(View.GONE);
                mOrderListEcOverDone.setVisibility(View.VISIBLE);
                mOrderListEcOverDone.setAdapter(la);
            }
        }
    }

    private void getOrderListHuoZhu(boolean isLoading) {
        if(isLoading){
            startLoadingDialog();
        }
        RequestService.getInstance().getCargoOrderShipperList(this, size + "", cursor + "", action, searchstatus + "", OrderListEntity.class, new HttpRequest.RequestListener() {

            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if (resultData.isOk()) {
                    OrderListEntity entity = (OrderListEntity) resultData;
                    List<Order> list = entity.getData().getList();
                    mDataListWuLiu.addAll(list);

                    if ((mDataListWuLiu.size() < size) || (action.equals(ListViewAction.HISTORY) && list.size() < size)) {
                        if (TAB_WL_INPROGRESS.equals(currentTabTag)) {
                            mOrderListWlInProgressview.endLoadingMore("没有更多了");
                        } else if (TAB_WL_OVER_DONE.equals(currentTabTag)) {
                            mOrderListWlOverDoneview.endLoadingMore("没有更多了");
                        } else if (TAB_WL_INVALID.equals(currentTabTag)) {
                            mOrderListWlInvalidview.endLoadingMore("没有更多了");
                        }
                    }
                    setAdapter(mDataListWuLiu);

                } else {
                    showToast(resultData.getMsg());
                }
                onLoad();
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(msg);
                onLoad();
            }
        });
    }


    private void getOrderListCheZhu(boolean isLoading) {
        if(isLoading){
            startLoadingDialog();
        }
        RequestService.getInstance().getCargoOrderCarList(this, size + "", cursor + "", action, searchstatus + "", OrderListEntity.class, new HttpRequest.RequestListener() {

            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if (resultData.isOk()) {
                    OrderListEntity entity = (OrderListEntity) resultData;
                    List<Order> list = entity.getData().getList();
                    mDataListWuLiu.addAll(list);

                    if ((mDataListWuLiu.size() < size) || (action.equals(ListViewAction.HISTORY) && list.size() < size)) {
                        if (TAB_WL_INPROGRESS.equals(currentTabTag)) {
                            mOrderListWlInProgressview.endLoadingMore("没有更多了");
                        } else if (TAB_WL_OVER_DONE.equals(currentTabTag)) {
                            mOrderListWlOverDoneview.endLoadingMore("没有更多了");
                        } else if (TAB_WL_INVALID.equals(currentTabTag)) {
                            mOrderListWlInvalidview.endLoadingMore("没有更多了");
                        }
                    }
                    setAdapter(mDataListWuLiu);

                } else {
                    showToast(resultData.getMsg());
                }
                onLoad();
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(msg);
                onLoad();
            }

        });
    }


    private void onLoad() {
        if (TAB_WL_INPROGRESS.equals(currentTabTag)) {
            onLoad(mOrderListWlInProgressview);
        } else if (TAB_WL_OVER_DONE.equals(currentTabTag)) {
            onLoad(mOrderListWlOverDoneview);
        } else if (TAB_WL_INVALID.equals(currentTabTag)) {
            onLoad(mOrderListWlInvalidview);
        } else if (TAB_DS_WAIT_PAY.equals(currentTabTag)) {
            onLoad(mOrderListEcWaitToPay);
        } else if (TAB_DS_WAIT_SEND.equals(currentTabTag)) {
            onLoad(mOrderListEcWaitSend);
        } else if (TAB_DS_HAVE_SEND.equals(currentTabTag)) {
            onLoad(mOrderListEcHaveSend);
        } else if (TAB_DS_OVER_DONE.equals(currentTabTag)) {
            onLoad(mOrderListEcOverDone);
        }

    }


    private void onLoad(XListView mOrderListview) {
        mOrderListview.stopRefresh();
        mOrderListview.stopLoadMore();
    }

    @Override
    public void initListener() {
        textCheZhu.setOnClickListener(this);
        textHuoZhu.setOnClickListener(this);
        textCaiGou.setOnClickListener(this);
        textGongYing.setOnClickListener(this);
        tabHost.setOnTabChangedListener(this);
        setListView(mOrderListWlInProgressview);
        setListView(mOrderListWlOverDoneview);
        setListView(mOrderListWlInvalidview);

        setListView(mOrderListEcHaveSend);
        setListView(mOrderListEcOverDone);
        setListView(mOrderListEcWaitSend);
        setListView(mOrderListEcWaitToPay);
    }


    private void setListView(XListView listView) {
        listView.setPullLoadEnable(true);
        listView.setPullRefreshEnable(true);
        listView.setXListViewListener(this);
        listView.setOnItemClickListener(this);
    }

    //只用于物流  车主、货主
    protected void setAdapter(List<Order> mDataList2) {
        if (textHuoZhu.isSelected()) {
            if (TAB_WL_INPROGRESS.equals(currentTabTag)) {
                if (mDataList2.size() == 0) {
                    emptyWlInprogress.setVisibility(View.VISIBLE);
                    mOrderListWlInProgressview.setVisibility(View.GONE);
                } else {
                    emptyWlInprogress.setVisibility(View.GONE);
                    mOrderListWlInProgressview.setVisibility(View.VISIBLE);
                    freshListAdapter(mAdapterCarHuoZhu, mOrderListWlInProgressview, mDataList2);
                }
            } else if (TAB_WL_OVER_DONE.equals(currentTabTag)) {
                if (mDataList2.size() == 0) {
                    emptyWlOverDone.setVisibility(View.VISIBLE);
                    mOrderListWlOverDoneview.setVisibility(View.GONE);
                } else {
                    emptyWlOverDone.setVisibility(View.GONE);
                    mOrderListWlOverDoneview.setVisibility(View.VISIBLE);
                    freshListAdapter(mAdapterCarHuoZhu, mOrderListWlOverDoneview, mDataList2);
                }
            } else if (TAB_WL_INVALID.equals(currentTabTag)) {
                if (mDataList2.size() == 0) {
                    emptyWlInvalid.setVisibility(View.VISIBLE);
                    mOrderListWlInvalidview.setVisibility(View.GONE);
                } else {
                    emptyWlInvalid.setVisibility(View.GONE);
                    mOrderListWlInvalidview.setVisibility(View.VISIBLE);
                    freshListAdapter(mAdapterCarHuoZhu, mOrderListWlInvalidview, mDataList2);
                }
            }
        } else if (textCheZhu.isSelected()) {
            if (TAB_WL_INPROGRESS.equals(currentTabTag)) {
                if (mDataList2.size() == 0) {
                    emptyWlInprogress.setVisibility(View.VISIBLE);
                    mOrderListWlInProgressview.setVisibility(View.GONE);
                } else {
                    emptyWlInprogress.setVisibility(View.GONE);
                    mOrderListWlInProgressview.setVisibility(View.VISIBLE);
                    freshListAdapter(mAdapterCarCheZhu, mOrderListWlInProgressview, mDataList2);
                }
            } else if (TAB_WL_OVER_DONE.equals(currentTabTag)) {
                if (mDataList2.size() == 0) {
                    emptyWlOverDone.setVisibility(View.VISIBLE);
                    mOrderListWlOverDoneview.setVisibility(View.GONE);
                } else {
                    emptyWlOverDone.setVisibility(View.GONE);
                    mOrderListWlOverDoneview.setVisibility(View.VISIBLE);
                    freshListAdapter(mAdapterCarCheZhu, mOrderListWlOverDoneview, mDataList2);
                }
            } else if (TAB_WL_INVALID.equals(currentTabTag)) {
                if (mDataList2.size() == 0) {
                    emptyWlInvalid.setVisibility(View.VISIBLE);
                    mOrderListWlInvalidview.setVisibility(View.GONE);
                } else {
                    emptyWlInvalid.setVisibility(View.GONE);
                    mOrderListWlInvalidview.setVisibility(View.VISIBLE);
                    freshListAdapter(mAdapterCarCheZhu, mOrderListWlInvalidview, mDataList2);
                }
            }
        }

    }

    /**
     * 车主列表
     * @param mAdapter
     * @param mOrderListview
     * @param mDataList2
     */
    private void freshListAdapter(OrderShipperListAdapter mAdapter, XListView mOrderListview, List<Order> mDataList2) {
        if(mAdapter == null){
            mAdapter = new OrderShipperListAdapter<Order>(this, mDataList2, false);
            mOrderListview.setAdapter(mAdapter);
        }else{
            mAdapter.setData(mDataList2);
        }
    }

    /**
     * 货主列表
     * @param mAdapter
     * @param mOrderListview
     * @param mDataList2
     */
    private void freshListAdapter(OrderListAdapter mAdapter, XListView mOrderListview, List<Order> mDataList2) {
        if(mAdapterCarHuoZhu == null){
            mAdapter = new OrderListAdapter<Order>(this, mDataList2, false);
            mOrderListview.setAdapter(mAdapter);
        }else{
            mAdapter.setData(mDataList2);
        }
    }

    @Override
    public void onRefresh() {
        cursor = "0";
        mDataListWuLiu.clear();
        mDataListCaiGou.clear();
        mDataListGongYing.clear();
        action = ListViewAction.NEW;
        actionGongYing = ListViewAction.NEW2;
        if (mAdapterCaiGou != null) {
            mAdapterCaiGou.setData(mDataListCaiGou);
        }
        if (mAdapterCarHuoZhu != null) {
            mAdapterCarHuoZhu.setData(mDataListWuLiu);
        }
        if (mAdapterCarCheZhu != null) {
            mAdapterCarCheZhu.setData(mDataListWuLiu);
        }
        if (mAdapterGongYing != null) {
            mAdapterGongYing.setData(mDataListGongYing);
        }

        if (TAB_WL_INPROGRESS.equals(currentTabTag)) {
            searchstatus = STATUS_IN_PROGRESS;
            mOrderListWlInProgressview.setPullLoadEnable(true);
        } else if (TAB_WL_OVER_DONE.equals(currentTabTag)) {
            searchstatus = STATUS_OVERDONE;
            mOrderListWlOverDoneview.setPullLoadEnable(true);
        } else if (TAB_WL_INVALID.equals(currentTabTag)) {
            searchstatus = STATUS_INVALID;
            mOrderListWlInvalidview.setPullLoadEnable(true);
        } else if (TAB_DS_WAIT_PAY.equals(currentTabTag)) {
            searchstatus = STATUS_DAIFUKUAN;
            mOrderListEcWaitToPay.setPullLoadEnable(true);
        } else if (TAB_DS_WAIT_SEND.equals(currentTabTag)) {
            searchstatus = STATUS_DAIFAHUO;
            mOrderListEcWaitSend.setPullLoadEnable(true);
        } else if (TAB_DS_HAVE_SEND.equals(currentTabTag)) {
            if (currentOrderType == ORDER_TYPE_CAIGOU) {
                searchstatus = STATUS_YISHOUHUO;
            } else if (currentOrderType == ORDER_TYPE_GONGYING) {
                searchstatus = STATUS_DAISHOUHUO;
            }
            mOrderListEcHaveSend.setPullLoadEnable(true);
        } else if (TAB_DS_OVER_DONE.equals(currentTabTag)) {
            searchstatus = STATUS_FINISH;
            mOrderListEcOverDone.setPullLoadEnable(true);
        }
        initData();
    }

    @Override
    public void onLoadMore() {
        if(mDataListWuLiu.size()>0){
            cursor = mDataListWuLiu.get(mDataListWuLiu.size()-1).getCursor();
        } else if (mDataListCaiGou.size() > 0) {
            cursor = mDataListCaiGou.get(mDataListCaiGou.size()-1).getCursor();
        } else {
            cursor = "";
        }
        action = ListViewAction.HISTORY;
        initData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.textview_huozhu:
//                textCheZhu.setSelected(false);
//                textHuoZhu.setSelected(true);
                selectTabhost(ORDER_TYPE_HUOZHU, currentTabTag);
//                onRefresh();
                break;
            case R.id.textview_chezhu:
//                textCheZhu.setSelected(true);
//                textHuoZhu.setSelected(false);
                selectTabhost(ORDER_TYPE_CHEZHU, currentTabTag);
//                onRefresh();
                break;
            case R.id.textview_caigou:
                selectTabhost(ORDER_TYPE_CAIGOU, currentTabTag);
//                onRefresh();
                break;
            case R.id.textview_gongying:
                selectTabhost(ORDER_TYPE_GONGYING, currentTabTag);
//                onRefresh();
                break;
            default:
                break;
        }
    }

    @Override
    public void onTabChanged(String tabId) {
//        ViewHelper.setTabHostStyle(tabHost.getTabWidget(), this, tabHost.getCurrentTab());
        selectTabhost(currentOrderType, tabId);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (parent == mOrderListWlInProgressview || parent == mOrderListWlOverDoneview || parent == mOrderListWlInvalidview) {
            Order order = (Order) parent.getItemAtPosition(position);
            Class targetClass = null;
            if (textHuoZhu.isSelected()) {
                targetClass = OrderDetailShipperActivity.class;
            } else if (textCheZhu.isSelected()) {
                targetClass = OrderDetailActivity.class;
            }
            Intent intent = new Intent(this,targetClass);
            intent.putExtra(ExtraName.orderid, order.getOrderid());
            startActivity(intent);
        } else if (parent == mOrderListEcWaitToPay ||
                parent == mOrderListEcWaitSend ||
                parent == mOrderListEcHaveSend ||
                parent == mOrderListEcOverDone) {
            if (currentOrderType == ORDER_TYPE_CAIGOU) {
                PurchaseOrder order = (PurchaseOrder) parent.getItemAtPosition(position);
                if("0".equals(order.getStatus())){
                    Intent intent = new Intent(OrderManagerActivity.this,PurchaseRecordDetailActivity.class);
                    intent.putExtra(ExtraName.id, order.getOrderid());
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(OrderManagerActivity.this, PurchaseOrderDetailActivity.class);
                    intent.putExtra(ExtraName.orderid, order.getOrderid());
                    startActivity(intent);
                }
            } else if (currentOrderType == ORDER_TYPE_GONGYING) {
                SupplyOrder order = (SupplyOrder) parent.getItemAtPosition(position);
                Intent intent = new Intent(OrderManagerActivity.this, SupplyOrderDetailActivity.class);
                intent.putExtra(ExtraName.orderid, order.getOrderid());
                startActivity(intent);
            }
        }

//        private XListView mOrderListWlInProgressview;
//        private XListView mOrderListWlOverDoneview;
//        private XListView mOrderListWlInvalidview;
//
//        private XListView mOrderListEcWaitToPay;
//        private XListView mOrderListEcWaitSend;
//        private XListView mOrderListEcHaveSend;
//        private XListView mOrderListEcOverDone;


    }
}
